﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Unitity;
using Microsoft.Xna.Framework;
using Microsoft.Devices.Sensors;
using Spinning.Spinning.Entity.Items;

namespace Spinning.Spinning.Entity.Scenes.SinglePlayer
{
    class SinglePlayerStage2Scene : SinglePlayerScene
    {
        List<Vector2> batPosList = new List<Vector2>();
        List<Vector2> broomPosList = new List<Vector2>();
        List<Vector2> deadBranchPosList = new List<Vector2>();
        List<Vector2> spiderWebPosList = new List<Vector2>();
        List<Vector2> stonePosList = new List<Vector2>();
        List<Vector2> tornadoPosList = new List<Vector2>();
        List<Vector2> accPosList = new List<Vector2>();
        List<Vector2> scorePosList = new List<Vector2>();

        public override void OnLoad()
        {
            base.OnLoad();
            ContinuousSprite backGroundSprite = new ContinuousSprite(Camera, 1);
            backGroundSprite.textureName = "singleStage2/k1_background";
            backGroundSprite.ZIndex = Constants.background;
            backGroundSprite.OnLoad();
            this.Attach(backGroundSprite);

            Player.LifeTime = Game1.stageManager.LifeTimeEarnByUser * 5;

            setPosList();

            for (int i = 0; i < batPosList.Count; i++)
            {
                DynamicBarrier bat = new DynamicBarrier(this.Player);
                bat.textureName = "singleStage2/bat";
                bat.OnLoad();
                bat.ItemLevel = 3; //
                bat.SetAnimation(new Point(1, 1));
                bat.OnCreatBody(World, PhysicConnectManager);
                bat.BodyPosiotion = batPosList[i];
                this.Attach(bat);
            }

            for (int i = 0; i < broomPosList.Count; i++)
            {
                StaticBarrier broom = new StaticBarrier();
                broom.textureName = "singleStage2/broom";
                broom.OnLoad();
                broom.ItemLevel = 2; //
                broom.SetAnimation(new Point(1, 1));
                broom.OnCreatBody(World, PhysicConnectManager);
                broom.BodyPosiotion = broomPosList[i];
                this.Attach(broom);
            }

            for (int i = 0; i < tornadoPosList.Count; i++)
            {
                DynamicBarrier tornado = new DynamicBarrier(this.Player);
                tornado.textureName = "singleStage2/tornado";
                tornado.OnLoad();
                tornado.ItemLevel = 3; //
                tornado.SetAnimation(new Point(1, 1));
                tornado.OnCreatBody(World, PhysicConnectManager);
                tornado.BodyPosiotion = tornadoPosList[i];
                this.Attach(tornado);
            }

            for (int i = 0; i < deadBranchPosList.Count; i++)
            {
                StaticBarrier deadBranch = new StaticBarrier();
                deadBranch.textureName = "singleStage2/dead_branch";
                deadBranch.OnLoad();
                deadBranch.ItemLevel = 2;
                deadBranch.SetAnimation(new Point(1, 1));
                deadBranch.OnCreatBody(World, PhysicConnectManager);
                deadBranch.BodyPosiotion = deadBranchPosList[i];
                this.Attach(deadBranch);
            }

            for (int i = 0; i < spiderWebPosList.Count; i++)
            {
                StaticBarrier spiderWeb = new StaticBarrier();
                spiderWeb.textureName = "singleStage2/spider_web";
                spiderWeb.OnLoad();
                spiderWeb.ItemLevel = 2;
                spiderWeb.SetAnimation(new Point(1, 1));
                spiderWeb.OnCreatBody(World, PhysicConnectManager);
                spiderWeb.BodyPosiotion = spiderWebPosList[i];
                this.Attach(spiderWeb);
            }

            for (int i = 0; i < stonePosList.Count; i++)
            {
                StaticBarrier stone = new StaticBarrier();
                stone.textureName = "singleStage2/stone";
                stone.OnLoad();
                stone.ItemLevel = 2;
                stone.isCircle = true;  //寫在OnCreatBody前
                stone.SetAnimation(new Point(1, 1));
                stone.OnCreatBody(World, PhysicConnectManager);
                stone.BodyPosiotion = stonePosList[i];
                this.Attach(stone);
            }

            for (int i = 0; i < accPosList.Count; i++)
            {
                AddTimeItem accItem = new AddTimeItem();
                accItem.textureName = "singleStage1/accelerate";
                accItem.OnLoad();
                accItem.ItemLevel = 5;
                accItem.SetAnimation(new Point(1, 1));
                accItem.OnCreatBody(World, PhysicConnectManager);
                accItem.BodyPosiotion = accPosList[i];
                this.Attach(accItem);
            }

            for (int i = 0; i < scorePosList.Count; i++)
            {
                AddScoreItem score = new AddScoreItem();
                score.textureName = "singleStage1/score";
                score.OnLoad();
                score.SetAnimation(new Point(1, 1));
                score.OnCreatBody(World, PhysicConnectManager);
                score.BodyPosiotion = scorePosList[i];
                score.OnGetScoreItemEvent += test;
                this.Attach(score);
            }

            Bound b1 = new Bound();
            b1.Size = new Vector2(Constants.Window_Width, 20);
            b1.OnCreatBound(World);
            b1.Position = new Vector2(Constants.Window_Width / 2, 800);//下界
            Attach(b1);

            Bound b2 = new Bound();
            b2.Size = new Vector2(Constants.Window_Width, 20);
            b2.OnCreatBound(World);
            b2.Position = new Vector2(Constants.Window_Width / 2, -17000);//上界
            Attach(b2);

            SortChildren();
        }

        public void test() //測試 加分數事件
        {
            Player.AddLifeTime(0);
        }

        public override Scene Clone()
        {
            Scene scene = new SinglePlayerStage2Scene();
            scene.Size = new Vector2(Constants.Window_Width, Constants.Window_Height);
            scene.OnLoad();
            scene.SortChildren();
            return scene;
        }

        void setPosList()
        {
            int[] posX = new int[] { 60, 180, 300, 420 };

            Random random = new Random();

            Vector2 v;
            for (int i = 0; i < 40; i++)
            {
                v = new Vector2(posX[random.Next(0, 4)], -400 * i);
                switch (i % 10)
                {
                    case 0:
                        batPosList.Add(v);
                        break;
                    case 1:
                        accPosList.Add(v);
                        break;
                    case 2:
                        broomPosList.Add(v);
                        break;
                    case 3:
                        deadBranchPosList.Add(v);
                        break;
                    case 4:
                        scorePosList.Add(v);
                        break;
                    case 5:
                        spiderWebPosList.Add(v);
                        break;
                    case 6:
                        accPosList.Add(v);
                        break;
                    case 7:
                        stonePosList.Add(v);
                        break;
                    case 8:
                        scorePosList.Add(v);
                        break;
                    case 9:
                        tornadoPosList.Add(v);
                        break;
                }

            }
        }
    }
}
